$(function(){
	$("#roomHistogram").parent().click(function(){window.location.href='reception.zul';});
	$("#makeCard").parent().click(onMakeCard);
	$("#ecycleCard").parent().click(onRecycleCard);
	$("#nightAudit").parent().click(onNightAudit);
});

function onNightAudit() {
	var dialog = $("#nightAuditDialog");
	AdminAjax.getLastNightAuditInfo(function(data){
		if (data == null)
			data = "暂未夜审";
		$("#lastAuditInfo").text(data);
	});
	dialog.dialog({
		autoOpen: true,
		height: 200,
		width: 350,
		modal: false,
		title: "夜审",
		buttons: {
			'夜审': function() {
				if (!confirm("\n系统每天在深夜自动进行夜审, 正常情况下不应该人工进行手动调用; " + 
						"只有当在夜审时系统发生异常时才使用此功能.\n\n " + 
						"你确认要进行些操作吗?", false))
					return;
				var p = prompt("确认请输入 'Yes', (不含引号, 不区分大小写)", "no");
				if (p.toLowerCase() != 'yes')
					return;
				AdminAjax.nightAudit(function(data){
					if (data != null) {
						alert(data);
						return;
					}
					alert("夜审成功");
				});
			},
			'关闭': function() {
				$(this).dialog('close');
			}
		}
	});
}

function onMakeCard() {
	var dialog = $("#makeCardDialog");
	$("ul.left", dialog).html("");
	$("ul.right", dialog).html("");
	$("input:text", dialog).val("");
	$("#makeCardRoom", dialog).unbind("change").bind("change", function(){
		var room = $(this).val();
		if (room != '') {
			CardAjax.getRoomCardStatus(room, function(data){
				show(data, dialog);
			});
		}
	});
	dialog.dialog({
		autoOpen: true,
		height: 350,
		width: 600,
		modal: false,
		title: "制卡",
		buttons: {
			'刷卡': function() {
				var d = Card.readDetail();
				if (d == null) {
					show("请把卡放在读卡器上", dialog);
					return;
				}
				dialog[0].card = d[0];
				$("ul.left", dialog).html("");
				$("ul.right", dialog).html("");
				var html = "";
				html += "<li><label>序列号</label>&nbsp;" + d[0] + "</li>";
				html += "<li><label>锁号</label>&nbsp;" + d[1] + "</li>";
				html += "<li><label>有效期</label>&nbsp;" + d[2] + "</li>";
				$("ul.left", dialog).html(html);
				CardAjax.getCardDetailBySerialNumber(d[0], function(data){
					$("ul.right", dialog).html(data);
					$("ul.edit", dialog).css("display", "");
				});
			},
			'制卡': function(){
				var serial = dialog[0].card;
				if (serial == null)
					return show("请先刷卡", dialog);
				var room = $("#makeCardRoom", dialog).val();
				if (room == '')
					return show("请输入房间号", dialog);
				var time = $('#makeCardTime').val();
				if (time.length < 8)
					return show("授权时间输入不合法", dialog);
				if(!Card.check(serial))
					return show("请不要把卡从读卡器上移走", dialog);
				CardAjax.makeCard(serial, room, time, {callback:function(data){
					if (data == null)
						return;
					if (Card.writeDirectly(data[0], data[1]))
						alert("制卡成功");
					else
						alert("制卡失败, 但系统中已经保存成功, 请重试");
				},errorHandler:function(msg, e){
					show(msg, dialog);
				}});
			},
			'关闭': function() {
				$(this).dialog('close');
			}
		}
	});
}

function onRecycleCard() {
	var dialog = $("#ecycleCardDialog");
	$("ul.left", dialog).html("");
	$("ul.right", dialog).html("");
	$("ul.edit", dialog).css("display", "none");
	hide(dialog);
	dialog[0].card = null;
	dialog.dialog({
		autoOpen: true,
		height: 315,
		width: 600,
		modal: false,
		title: "回收卡",
		buttons: {
			'刷卡': function() {
				$(".ui-widget", dialog).hide();
				var d = Card.readDetail();
				if (d == null) {
					show("请把卡放在读卡器上", dialog);
					return;
				}
				dialog[0].card = d[0];
				$("ul.left", dialog).html("");
				$("ul.right", dialog).html("");
				var html = "";
				html += "<li><label>序列号</label>&nbsp;" + d[0] + "</li>";
				html += "<li><label>锁号</label>&nbsp;" + d[1] + "</li>";
				html += "<li><label>有效期</label>&nbsp;" + d[2] + "</li>";
				$("ul.left", dialog).html(html);
				CardAjax.getCardDetailBySerialNumber(d[0], function(data){
					$("ul.right", dialog).html(data);
					$("ul.edit", dialog).css("display", "");
				});
			},
			'销卡': function() {
				var serial = dialog[0].card;
				if (serial == null)
					return;
				if (confirm("您确认要收回此卡的开房授权?")){
					if (Card.write(serial, '0','0000000000')) {
						CardAjax.cancelCard(serial, function(data){
							if (data) {
								alert("销卡成功");
							} else {
								alert("销卡成功, 但系统中没有相应的卡记录");
							}
						});
					} else {
						alert("销卡失败, 请重试");
					}
				}
			},
			'回收': function() {
				var serial = dialog[0].card;
				if (serial == null)
					return;
				if (confirm("您确认要收回此卡?")){
					if (Card.write(serial, '0','0000000000')) {
						CardAjax.recycleCard(serial, function(data){
							if (data) {
								alert("回收卡成功, 此卡已经不属于任何会员/员工, 如果已经授权为房卡, 也被收回");
							} else {
								alert("销卡成功, 但系统中没有相应的卡记录");
							}
						});
					} else {
						alert("销卡失败, 请重试");
					}
				}
			},
			'关闭': function() {
				$(this).dialog('close');
			}
		}
	});
}

///////////////////////////////////////////////////////////////////////////
function show(text, context) {
	$(".result", context).text(text);
	$(".ui-widget", context).show();
}
//
function hide(context){
	$(".result", context).text("");
	$(".ui-widget", context).hide();
}
